import 'package:demo1/pages/message/ui_chatdetail.dart';
import 'package:flutter/material.dart';
import 'message/ui_conversationlist.dart';

class ChatPage extends StatefulWidget {
  const ChatPage({Key? key}) : super(key: key);
  @override
  _ChatPageState createState() => _ChatPageState();
}

class _ChatPageState extends State<ChatPage> {
  List<Widget> list = <Widget>[Text("..")];
  int index = 0;

  var userChatDetails = <String, UiChatdetail>{};
  var userIds = <String>[];

  @override
  Widget build(BuildContext context) {
    final _screen = MediaQuery.of(context).size;
    var chatlist = UIConversationList();

    var items = Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(width: _screen.width * 0.25, child: chatlist),
        VerticalDivider(width: 1.0),
        Expanded(child: list[index]),
      ],
    );

    chatlist.onPress = (item) {
      if (userIds.contains(item.userId) == false) {
        userIds.add(item.userId);

        var uc = UiChatdetail(
          data: item,
        );
        userChatDetails[item.userId] = uc;

        list.add(uc);
        setState(() {
          index = list.length - 1;
        });
      } else {
        setState(() {
          index = userIds.indexOf(item.userId) + 1;
        });
      }
    };

    return items;
  }
}
